/* This DOS utility converts a binary file into DB statements for use in *
 * assemblers such as AVOCET's AVMACZ80. DB statements use more space    *
 * compared to DW statements, but may be more readable.                  *
 *				written by Paul Hutchinson								  */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dos.h"


FILE	*infile, *outfile;  /* input & output files */


void DB(void)
{
	unsigned char byte,loop;
	unsigned char line[]={"                "};
	int filebyte;
	long location = 0;
	loop=0;
	while(1)
		{
			if ((filebyte=getc(infile)) != EOF)
			{
				byte=filebyte & 255;
				if (loop==0)
				{
					if (byte < 16)
						fprintf(outfile,"\tdb\t00%xh",byte);
					else
						fprintf(outfile,"\tdb\t0%xh",byte);
				}
				else
					if (byte < 16)
						fprintf(outfile,",00%xh",byte);
					else
						fprintf(outfile,",0%xh",byte);
				if ((byte < 32) || (byte > 127))
					byte=32;
				line[loop]=byte;
				if	(++loop > 7)
				{
					fprintf(outfile,"\t;%5lx %s\n",location++ * 8,line);
					loop=0;
				}
			}
			else
				break;

		}
}

int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		printf("DB: convert file\n");
		printf("Usage: DB infile outfile\n");
		return EXIT_FAILURE;
	}
	if ((infile = fopen(argv[1], "rb")) == NULL)
	{
		fprintf(stderr, "DB: Unable to open file: %s\n", argv[1]);
		return EXIT_FAILURE;
	}

	if ((outfile = fopen(argv[2], "w")) == NULL)
	{
		fprintf(stderr, "DB: Unable to create file: %s\n", argv[2]);
		return EXIT_FAILURE;
	}

	DB();

	fclose(infile);
	fclose(outfile);

	return EXIT_SUCCESS;
}




